package com.dal.module.account.platform_verify;

import com.dal.constant.ParamKey;
import com.dal.db.bean.ChannelApp;
import com.sojoys.artifact.exception.AbstractLogicModelException;
import com.sojoys.artifact.manager.JsonManager;
import com.sojoys.artifact.tools.ToolError;
import com.sojoys.artifact.tools.ToolSimpleHttp;
import com.xiaoleilu.hutool.collection.CollUtil;

import org.apache.commons.collections4.MapUtils;
import org.error.LoginErrorCode;

import java.util.Map;

/**
 * Created by fxf on 2018-1-19.
 */
public class HeiTaoVerifyer implements PlatformVerifyer {

    private static String url = "http://smi.heitao.com/yhdzz/auth";
    private static final int ERR_NO_SUCCESS = 0;
    
    private static final int ERR_NO_NOT_TEST_PLAYER = 30007;
    @Override
    public void verify(ChannelApp channelApp,Map param) throws AbstractLogicModelException {
        String accountId = (String) param.get(ParamKey.ACCOUNT_ID);
        String token = (String) param.get(ParamKey.TOKEN);

        String paramStr = "uid="+accountId+"&token="+token;
        String result = ToolSimpleHttp.sendGet(url,paramStr);

        Map map = JsonManager.me().getJson().parse(result,Map.class);
        
        Map data = MapUtils.getMap(map, "data");
        if (!CollUtil.isEmpty(data)) {
			String phone = MapUtils.getString(data, ParamKey.MOBILE);
			param.put(ParamKey.MOBILE, phone);
			int betaType = MapUtils.getInteger(data,ParamKey.DAL_BETA_TYPE);
            param.put(ParamKey.DAL_BETA_TYPE, betaType);
		}
        
        int errno = MapUtils.getIntValue(map, "errno", -1);
        int localError = LoginErrorCode.REMOTE_VERIFYER_ERROR;
        if (errno == ERR_NO_NOT_TEST_PLAYER) {
        	localError = LoginErrorCode.NOT_TEST_PLAYER;
		}
        ToolError.isAndTrue(localError ,MapUtils.getString(map, "errmsg", "平台登录异常"),errno != ERR_NO_SUCCESS);
    }
    
    public static void main(String[] args) {
    	 String paramStr = "uid="+32553986+"&token="+"d15187b216d91275320eb94e4e5a841a";
    	System.out.println(ToolSimpleHttp.sendGet(url,paramStr));
	}
}
